# This code makes figures showing the relationship between public support for policies and their implementation

# Set working directory to project folder (assign to wd)
setwd(wd)

library(foreign)
# Load policy implementation dataset
dat <- read.csv("data/implementationdata.csv")


# Get vector of election years
election_year <- c(1956, 1960, 1964, 1968, 1973, 1976, 1979, 1982, 
                   1985, 1988, 1991, 1994, 1998, 2002, 2006, 2010, 2014)

# Figure A1 (a)
for(j in 1:length(unique(dat$org_var))){
             # Calculate differences in proportions supporting governing parties
      if(dat$year[which(dat$org_var==dat$org_var[j])] %in% c(1936:1973,1982:1988,1994:2002,2014)){
        dat$govsupport[dat$org_var==dat$org_var[j]] <- dat$ps_fvr_dk[dat$org_var==dat$org_var[j]] # Proportion who support the change among supporters of govt parties
        dat$pmsupport[dat$org_var==dat$org_var[j]] <- dat$ps_fvr_dk[dat$org_var==dat$org_var[j]] # Proportion who support the change among supporters of PM party
         }
      if(dat$year[which(dat$org_var==dat$org_var[j])]==1976){
        dat$pmsupport[dat$org_var==dat$org_var[j]] <- 0.68*dat$pc_fvr_dk[dat$org_var==dat$org_var[j]] + 0.32*dat$pfp_fvr_dk[dat$org_var==dat$org_var[j]]  # weight by proportion of time as pm
        dat$govsupport[dat$org_var==dat$org_var[j]] <- dat$pallians_fvr_dk[dat$org_var==dat$org_var[j]]
      }# Note that I include all parties that led the government at least once during the mandate since they had a chance to change policy
      if(dat$year[which(dat$org_var==dat$org_var[j])]==1979){
        dat$pmsupport[dat$org_var==dat$org_var[j]] <- dat$pc_fvr_dk[dat$org_var==dat$org_var[j]]
        dat$govsupport[dat$org_var==dat$org_var[j]] <- dat$pallians_fvr_dk[dat$org_var==dat$org_var[j]]
              }
  if(dat$year[which(dat$org_var==dat$org_var[j])] %in% c(1991,2006,2010)){
        dat$pmsupport[dat$org_var==dat$org_var[j]] <- dat$pm_fvr_dk[dat$org_var==dat$org_var[j]]
        dat$govsupport[dat$org_var==dat$org_var[j]] <- dat$pallians_fvr_dk[dat$org_var==dat$org_var[j]]
        
      }

  var <- paste0("year",0:4,"policy") # Get all relevant policy variables
  dat$policy[dat$org_var==dat$org_var[j]] <- ifelse(mean(dat[dat$org_var==dat$org_var[j],var]==1,na.rm=T)!=0,1,0)# If a proportion greater than 0 takes the value of 1 code this variable 1
 }

dat$gw_fvr_dk <- dat$gw_fvr_dk*100
# Overall support
implementation <- vector()
for(i in seq(0,90,10)){
 implementation <- c(implementation, mean(dat$policy[which(dat$gw_fvr_dk>i-10 & dat$gw_fvr_dk<=i)]==1,na.rm=T))
}
df <- data.frame(gw_fvr_dk=seq(0,90,10),imp=100*implementation)
library(ggplot2)
pdf("overallrepall.pdf")
ggplot(df,aes(gw_fvr_dk,imp)) + geom_point() + geom_line() + theme_bw() + xlab("Policy Support (%)") + ylab("Implemented (%)") + labs(colour="Issue Voting") + scale_colour_grey() + geom_rug(data=dat,mapping=aes(gw_fvr_dk,0))
dev.off()


dat$govsupport <- dat$govsupport*100

# Figure A1 (b) Supporters of government party/parties
implementation <- vector()
for(i in seq(0,90,10)){
 implementation <- c(implementation, mean(dat$policy[which(dat$govsupport>i-10 & dat$gw_fvr_dk<=i)]==1,na.rm=T))
}
df <- data.frame(govsupport=seq(0,90,10),imp=100*implementation)
pdf("supportersrepall.pdf")
ggplot(df,aes(govsupport,imp)) + geom_point() + geom_line() + theme_bw() + xlab("Policy Support (%)") + ylab("Implemented (%)") + labs(colour="Issue Voting") + scale_colour_grey() + geom_rug(data=dat,mapping=aes(govsupport,0))
dev.off()

# Figure 2
# Election Studies only
dat <- dat[which(dat$survey=="vu"),]

# Overall support
implementation <- vector()
for(i in seq(0,90,10)){
 implementation <- c(implementation, mean(dat$policy[which(dat$gw_fvr_dk>i-10 & dat$gw_fvr_dk<=i)]==1,na.rm=T))
}
df <- data.frame(gw_fvr_dk=seq(0,90,10),imp=100*implementation)
library(ggplot2)
pdf("overallrep_snes.pdf")
ggplot(df,aes(gw_fvr_dk,imp)) + geom_point() + geom_line() + theme_bw() + xlab("Policy Support (%)") + ylab("Implemented (%)") + labs(colour="Issue Voting") + scale_colour_grey() + geom_rug(data=dat,mapping=aes(gw_fvr_dk,0)) + ylim(0,60)
dev.off()

# Supporters of government party/parties (note not in paper)
implementation <- vector()
for(i in seq(0,90,10)){
 implementation <- c(implementation, mean(dat$policy[which(dat$govsupport>i-10 & dat$govsupport<=i)]==1,na.rm=T))
}
df <- data.frame(govsupport=seq(0,90,10),imp=100*implementation)
pdf("supportersrep_snes.pdf")
ggplot(df,aes(govsupport,imp)) + geom_point() + geom_line() + theme_bw() + xlab("Policy Support (%)") + ylab("Implemented (%)") + labs(colour="Issue Voting") + scale_colour_grey() + geom_rug(data=dat,mapping=aes(govsupport,0)) + ylim(0,60)
dev.off()

# Figure A15 High income citizens
dat$pi90_fvr_dk <- dat$pi90_fvr_dk*100
implementation <- vector()
for(i in seq(0,90,10)){
  implementation <- c(implementation, mean(dat$policy[which(dat$pi90_fvr_dk>i-10 & dat$pi90_fvr_dk<=i)]==1,na.rm=T))
}
df <- data.frame(pi90_fvr_dk=seq(0,90,10),imp=100*implementation)
pdf("overallrep_snes_highincome.pdf")
ggplot(df,aes(pi90_fvr_dk,imp)) + geom_point() + geom_line() + theme_bw() + xlab("Policy Support (%)") + ylab("Implemented (%)") + labs(colour="Issue Voting") + scale_colour_grey() + geom_rug(data=dat,mapping=aes(govsupport,0)) + ylim(0,60)
dev.off()

